package com.qianfeng.smsplatform.strategy.service.impl;//



import com.qianfeng.smsplatform.common.constants.RabbitMqConsants;
import com.qianfeng.smsplatform.common.constants.StrategyConstants;
import com.qianfeng.smsplatform.common.model.Standard_Report;
import com.qianfeng.smsplatform.common.model.Standard_Submit;
import com.qianfeng.smsplatform.strategy.feign.RedisCacheFeign;
import com.qianfeng.smsplatform.strategy.mq.SendMq;
import com.qianfeng.smsplatform.strategy.service.FilterChain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.StringReader;

/**
 * 敏感词过滤
 * Created by Mr.ycz on 2019/12/3 9:44
 */
@Service
public class SmsKeyWordFilter implements FilterChain {
    //记录推送次数
    private static Integer REPORT_COUNT=0;

    @Autowired
    private SendMq sendMq;
    @Autowired
    private RedisCacheFeign cacheFeign;

    @Override
    public boolean dealMessage(Standard_Submit submit) throws Exception {

        System.out.println("来了来了他来了................."+submit);

        //使用IK分词器分词
        StringReader stringReader = new StringReader(submit.getMessageContent());
        IKSegmenter ik = new IKSegmenter(stringReader,true );
        Lexeme lex;
        while ((lex=ik.next())!=null){

            System.out.println(lex.getLexemeText());

            //判断是否有敏感词
//            String isDirtyWords = cacheFeign.getString(CacheConstants.CACHE_PREFIX_DIRTYWORDS+lex.getLexemeText());
            String isDirtyWords = "哈哈";
            if(isDirtyWords!=null &&isDirtyWords=="1"){
                //存在敏感词 发送到mq
                sendMq.sendMq(RabbitMqConsants.TOPIC_SMS_SEND_LOG, submit);
                //如果同时是HTTP的发送方式就模拟出状态报告推送给客户
                if(submit.getSource() == 1){
                    Standard_Report report = new Standard_Report();
                    report.setErrorCode(StrategyConstants.STRATEGY_ERROR_DIRTYWORDS);
                    report.setClientID(submit.getClientID());
                    report.setMobile(submit.getDestMobile());
                    report.setMsgId(submit.getMsgid());
                    //推送次数
                    report.setSendCount(++REPORT_COUNT);
                    report.setSrcID(submit.getSrcSequenceId());
                    report.setState(2);
                    sendMq.sendMq(RabbitMqConsants.TOPIC_PUSH_SMS_REPORT,report);
                }
                //存在敏感词  不用继续查找
                return false;
            }
        }
        return true;
    }
}
